package personnal.blogprinter.generator;

import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.Set;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.log4j.Logger;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import personnal.blogprinter.data.PostInfo;
import personnal.blogprinter.exceptions.GenerationException;
import personnal.blogprinter.htmlparser.CountingElementsHandler;
import personnal.blogprinter.htmlparser.HtmlEntityResolver;

public class TestGenerator extends AbstractPdfGenerator {

    private static Logger logger = Logger.getLogger(TestGenerator.class);
    private XMLReader reader;
    private CountingElementsHandler saxHandler = new CountingElementsHandler();

    public TestGenerator() {
        super();
    }

    @Override
    public void init(File outputFile) throws GenerationException {
        try {
            SAXParserFactory parserFactory = SAXParserFactory.newInstance();
            EntityResolver htmlEntityResolver = new HtmlEntityResolver();
            parserFactory.setValidating(false);
            SAXParser parser = parserFactory.newSAXParser();
            this.reader = parser.getXMLReader();
            this.reader.setContentHandler(this.saxHandler);
            this.reader.setEntityResolver(htmlEntityResolver);
            this.reader.setErrorHandler(this.saxHandler);
            this.reader.setDTDHandler(this.saxHandler);
        } catch (ParserConfigurationException e) {
            throw new GenerationException("Can't add create xhtml parser", e);
        } catch (SAXException e) {
            throw new GenerationException("Can't add create xhtml parser", e);
        }
    }

    /**
     * @see personnal.blogprinter.generator.Generator#addPost(personnal.blogprinter.data.PostInfo)
     */
    public void addPost(PostInfo postInfo) throws GenerationException {
        logger.info("Adding post content to pdf document (postId=" + postInfo.getId() + ")");
        try {
            String content = postInfo.getContent();
            parsePostContent(content);
        } catch (IOException e) {
            throw new GenerationException("Can't get required image resource to generate pdf content for postId="
                    + postInfo.getId(), e);
        } catch (SAXException e) {
            throw new GenerationException("Can't add post content to pdf (postId=" + postInfo.getId() + ")", e);
        }
    }

    private void parsePostContent(String content) throws IOException, SAXException {
        InputSource is = new InputSource(new StringReader(
                HTML_DOCTYPE + "<html><body>\n" + content + "</body></html>"));
        this.reader.parse(is);
    }

    @Override
    public void destroy() {
        Set<String> tags = this.saxHandler.getTags();
        logger.info("Handled " + tags.size() + " tags in " + this.saxHandler.getDocumentCount() + " documents");
        for (String tagName : tags) {
            logger.info(tagName);
        }
    }
}
